// let querystring =require("querystring");
// let path = require('path');
// let fs =require('fs');
//
// let requestPromise = require('request-promise');
let request = require('request')
//
// // console.log('__dir',__dirname);
// // console.log('cwd',process.cwd());
// // console.log('root',path.resolve('./'));
//
// let option = {
//   method: 'GET',
//   followRedirect: false,
//   // uri: 'http://localhost:8080/website/automatic/api/rd/second',
//   // uri: 'http://localhost:8080/website/automatic/api/rd/first',
// }
//
// // requestPromise(option)
// //   .then((body) => {
// //     console.log('zyf body', body);
// //   })
// //   .catch(e => {
// //     console.log(e);
// //   })
//

function requestPromise(options) {
  return new Promise(((resolve, reject) => {
    request(options, (error, response, body) => {
      console.log(response.statusCode, response.headers);
      if (error) {
        reject(error);
      } else {
        resolve({response, body});
      }
    })
  }))
}

requestPromise({
  method: 'GET',
  followRedirect: false,
  // uri: 'http://localhost:8080/website/automatic/api/rd/second',
  url: 'http://localhost:3001/rd/first',
}).then(({response, body}) => {
  console.log(response.statusCode, response.headers);
  if (response.statusCode === 302) {
    let location = response.headers.location;
    let url = new URL(location);
    let search = url.search;
    console.log(search);
    search = search.replace(encodeURIComponent('/rd/first'), encodeURIComponent('http://wx.10086.cn/website/automatic/servicePwd/index.html'))
    console.log(search);
    url.search = search;
    console.log(url);
    console.log(url.toString());
  }
})

// request({
//   method: 'GET',
//   followRedirect: false,
//   // uri: 'http://localhost:8080/website/automatic/api/rd/second',
//   url: 'http://localhost:3001/rd/first',
// }, (error, response, body) => {
//   console.log(response.statusCode, response.headers);
//   if (response.statusCode === 302) {
//     let location = response.headers.location;
//     let url = new URL(location);
//     let search = url.search;
//     console.log(search);
//     search = search.replace(encodeURIComponent('/rd/first'), encodeURIComponent('http://wx.10086.cn/website/automatic/servicePwd/index.html'))
//     console.log(search);
//     url.search = search;
//     console.log(url);
//     console.log(url.toString());
//   }
// })
//
// const url = 'https://open.weixin.qq.com/connect/oauth2/authorize';
// const info = {
//   appid: 'wx43a850f87498127d',
//   redirect_uri: 'http://wx.10086.cn/website/automatic/servicePwd/index.html',
//   response_type: 'code',
//   scope: 'snsapi_base',
//   state: ''
// };
// // console.log(`${url}?${querystring.stringify(info)}#wechat_redirect`);